package com.iocoder.yudao.module.erp.controller.admin.stock;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.iocoder.yudao.module.erp.controller.admin.stock.vo.warehouse.ErpWarehousePageVO;
import com.iocoder.yudao.module.erp.controller.admin.stock.vo.warehouse.ErpWarehouseResponseVO;
import com.iocoder.yudao.module.erp.controller.admin.stock.vo.warehouse.ErpWarehouseSaveVo;
import com.iocoder.yudao.module.erp.model.warehouse.ErpWarehouseDO;
import com.iocoder.yudao.module.erp.service.warehouse.ErpWarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP 仓库")
@RestController
@RequestMapping("/erp/warehouse")
@Validated
public class ErpWarehouseController {

    @Resource
    private ErpWarehouseService erpWarehouseService;

    @GetMapping("/page")
    @Operation(summary = "查询仓库信息分页")
    @PreAuthorize("@ss.hasPermission('erp:warehouse:query')")
    public CommonResult<PageResult<ErpWarehouseResponseVO>> getWarehousePage(@Valid ErpWarehousePageVO erpWarehousePageVO) {
        PageResult<ErpWarehouseDO> pageResult = erpWarehouseService.getWarehousePage(erpWarehousePageVO);
        return success(BeanUtils.toBean(pageResult, ErpWarehouseResponseVO.class));
    }


    @PostMapping(path = "/create", consumes = "application/json", produces = "application/json")
    @Operation(summary = "创建仓库信息")
    @PreAuthorize("@ss.hasPermission('erp:warehouse:create')")
    public CommonResult<Long> createWarehouse(@Valid @RequestBody ErpWarehouseSaveVo productSaveReqVO) {
        return success(erpWarehouseService.createWarehouse(productSaveReqVO));
    }


    @PutMapping("/update")
    @Operation(summary = "更新仓库信息")
    @PreAuthorize("@ss.hasPermission('erp:warehouse:update')")
    public CommonResult<Boolean> updateWarehouse(@Valid @RequestBody ErpWarehouseSaveVo updateReqVO) {
        erpWarehouseService.updateWarehouse(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仓库信息详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:warehouse:query')")
    public CommonResult<ErpWarehouseResponseVO> getWarehouse(@RequestParam("id") Long id) {
        ErpWarehouseDO warehouseDO = erpWarehouseService.getWarehouse(id);
        return success(BeanUtils.toBean(warehouseDO, ErpWarehouseResponseVO.class));
    }
    
    
    @PutMapping("/update-default-status")
    @Operation(summary = "更新仓库默认状态")
    @Parameters({
            @Parameter(name = "id", description = "编号", required = true),
            @Parameter(name = "status", description = "状态", required = true)
    })
    public CommonResult<Boolean>updateWarehouseDefaultStatus(@RequestParam("id") Long id,
                                                             @RequestParam("defaultStatus") Boolean defaultStatus )  {

        erpWarehouseService.updateWarehouseDefaultStatus(id,defaultStatus);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仓库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:warehouse:delete')")
    public CommonResult<Boolean> deleteWarehouse(@RequestParam("id") Long id) {
        erpWarehouseService.deleteWarehouse(id);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仓库 Excel")
    @PreAuthorize("@ss.hasPermission('erp:warehouse:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarehouseExcel(@Valid ErpWarehousePageVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpWarehouseDO> list = erpWarehouseService.getWarehousePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "仓库.xls", "数据", ErpWarehouseResponseVO.class,
                BeanUtils.toBean(list, ErpWarehouseResponseVO.class));
    }
}
